package com.project_doctor.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.project_doctor.exception.SystemCode;
import com.project_doctor.pojo.Doctor;
import com.project_doctor.pojo.Dto.DoctorRequest;
import com.project_doctor.pojo.Role;
import com.project_doctor.service.DoctorService;
import com.project_doctor.service.RoleService;
import com.project_doctor.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("doctor")
public class DoctorController {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    DoctorService doctorService;
    //注入Rabbit模板类
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping("/getAllDoctors")
    public ResponseEntity<?> getAllDoctors(@RequestBody(required = false) DoctorRequest doctorRequest) {
        List<Doctor> list = doctorService.getAllDoctors(doctorRequest);
        return ResponseEntity.ok(list);
    }
    @PostMapping("/getDoctorByBatchId")
    public ResponseEntity<?> getDoctorByBatchId(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("receiverIds");
        List<Doctor> doctors = doctorService.getDoctorByBatchId(ids);
        return ResponseEntity.ok(doctors);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/{userId}")
    public Doctor getUserInfo(@PathVariable Long userId) {
        return doctorService.getUserById(userId);
    }

    /**
     * 更新用户状态
     */
    @PostMapping("/{userId}/status")
    public Map<String, Object> updateUserStatus(
            @PathVariable Long userId,
            @RequestBody Map<String, String> request) {

        String status = request.get("status");
        doctorService.updateUserStatus(userId, status);

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "用户状态更新成功");
        return result;
    }

    /**
     * 获取当前登录用户信息（从JWT令牌中解析用户ID）
     */
    @GetMapping("/current")
    public Doctor getCurrentUser(@RequestHeader("Authorization") String token) {
        // 从JWT令牌中获取用户ID
        String userId = jwtUtil.getValue(token,"userId");
        if (userId != null) {
            return doctorService.getUserById(Long.valueOf(userId));
        }
        return null;
    }

    @PutMapping("/online/{id}")
    public ResponseEntity<?> updateOnlineStatus(@PathVariable Long id) {
        LambdaUpdateWrapper<Doctor> wrapper = Wrappers.lambdaUpdate(Doctor.class)
                .eq(Doctor::getId, id)
                .set(Doctor::getStatus, "ONLINE");
        doctorService.update(null,wrapper);
        return ResponseEntity.ok(SystemCode.SUCCESS_ONLINE.getMessage());
    }
    @PutMapping("/offline/{id}")
    public ResponseEntity<?> updateOfflineStatus(@PathVariable Long id) {
        LambdaUpdateWrapper<Doctor> wrapper = Wrappers.lambdaUpdate(Doctor.class)
                .eq(Doctor::getId, id)
                .set(Doctor::getStatus, "OFFLINE");
        doctorService.update(null,wrapper);
        return ResponseEntity.ok(SystemCode.SUCCESS_OFFLINE.getMessage());
    }
    @GetMapping("/checkIfOnline/{id}")
    public ResponseEntity<?> checkIfOnline(@PathVariable Long id) {
        Doctor user = doctorService.getById(id);
        Boolean flag =user.getStatus().equals("ONLINE");
        log.info("是否上线结果："+flag);
        return ResponseEntity.ok(flag);
    }
}
